/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package maincz.tests.webapp.dao;

import java.util.List;
import java.util.logging.Logger;
import maincz.tests.webapp.entity.User;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 * DAO trida pro uzivatele systemu.
 * @author Main
 */
@Repository
@Transactional
public class UserDAO implements IUserDAO {

    @Autowired
    private SessionFactory sessionFactory;

    private static final Logger log = Logger.getLogger("MyLogger");
     
    @Override
    public User getUser(String login) {
        List users = sessionFactory.getCurrentSession()
                .createQuery("from User as user where user.login = ?")
                .setString(0, login)
                .list();
        if (users == null || users.isEmpty()) {
            return null;
        }
        return (User) users.get(0);       
    }
    
    @Override
    public User getUser(String login, String pwdHash) {
        List users = sessionFactory.getCurrentSession()
                .createQuery("from User as user where user.login = ? and user.password = ?")
                .setString(0, login).setString(1, pwdHash)
                .list();
        if (users == null || users.isEmpty()) {
            return null;
        }
        return (User) users.get(0);       
    }
    
}
